import 'dart:math';

import 'package:emby/model/media.dart';
import 'package:emby/pages/video/play.dart';
import 'package:emby/util/function.dart';
import 'package:emby/widget/cupertino_image.dart';
import 'package:flutter/material.dart';

class MediaDetail extends StatefulWidget {
  final Media media;
  MediaDetail(this.media);
  @override
  _MediaDetailState createState() => _MediaDetailState();
}

class _MediaDetailState extends State<MediaDetail> {
  Media detail;
  @override
  void initState() {
    Api.mediaDetail(id: widget.media.id).then((res) {
      setState(() {
        detail = res.data;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: 265.0, //展开高度
            floating: true, //随着滑动隐藏标题
            pinned: true, //固定在顶部
            snap: true,
            stretch: false,
            backgroundColor: Colors.white,
            stretchTriggerOffset: 100,
            elevation: 0,
            flexibleSpace: LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {
                final Size size = constraints.biggest;
                final double appBarHeight = size.height;
                final double t = (appBarHeight - kToolbarHeight) / (100 - kToolbarHeight);
                print(t);
                final double thumbLeft = ((292 - appBarHeight) / 5).clamp(0.0, 45.0);
                final double extraPadding = Tween<double>(begin: 10.0, end: 24.0).transform(t);
                final double logoHeight = appBarHeight - 1.5 * extraPadding;
                return Stack(
                  overflow: Overflow.visible,
                  children: <Widget>[
                    Container(
                      height: 221,
                      child: Stack(
                        children: <Widget>[
                          CupertinoExtendedImage(
                            "${Util.baseUrl}/Items/${widget.media.id}/Images/Backdrop",
                            width: double.infinity,
                            height: 221,
                            fit: BoxFit.cover,
                            boxShape: BoxShape.rectangle,
                          ),
                          Container(
                            width: double.infinity,
                            decoration: BoxDecoration(
                              gradient: LinearGradient(
                                begin: Alignment(0.0, 1.0),
                                end: Alignment(0.0, -0.4),
                                colors: <Color>[Color(0x60000000), Color(0x00000000)],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Positioned(
                      top: max(MediaQuery.of(context).padding.top + 10, appBarHeight - 160),
                      left: thumbLeft + 5,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 10),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Padding(
                              padding: EdgeInsets.only(top: 3),
                              child: GestureDetector(
                                onTap: () {
                                  Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                                    return VideoPlayer(detail);
                                  }));
                                },
                                child: CupertinoExtendedImage(
                                  "${Util.baseUrl}/Items/${widget.media.id}/Images/Primary",
                                  width: 20 * t.clamp(1.0, 10.0),
                                  height: 30 * t.clamp(1.0, 10.0),
                                  fit: BoxFit.cover,
                                  boxShape: BoxShape.rectangle,
                                  borderRadius: BorderRadius.circular(5),
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Padding(
                                  padding: EdgeInsets.only(top: 5),
                                  child: Text(
                                    widget.media.name,
                                    style: TextStyle(fontSize: 28 * t.clamp(0.4, 1.0), color: Colors.white),
                                  ),
                                ),
                                if (t > 1.1) ...[
                                  SizedBox(
                                    height: 5,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Icon(
                                        Icons.star,
                                        size: 18,
                                        color: Colors.redAccent,
                                      ),
                                      Text(
                                        "${detail == null ? "**" : detail.communityRating}",
                                        style: TextStyle(fontSize: 14, color: Colors.white),
                                      ),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Text(
                                        widget.media.productionYear.toString(),
                                        style: TextStyle(fontSize: 14, color: Colors.white),
                                      ),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Text(
                                        "添加于：${detail == null ? "**" : detail.dateCreated.format("Y-m-d")}",
                                        style: TextStyle(fontSize: 14, color: Colors.white),
                                      )
                                    ],
                                  ),
                                  SizedBox(
                                    height: 25,
                                  ),
                                ],
                                if (t > 5) ...[
                                  Container(
                                    height: 20,
                                    child: detail != null && detail.genreItems != null
                                        ? Row(
                                            children: detail.genreItems.map((GenreItem genre) {
                                              return GestureDetector(
                                                onTap: () {},
                                                child: Text(
                                                  "${genre.name}${detail.genreItems.last != genre ? "," : ""}",
                                                  style: TextStyle(fontSize: 14),
                                                ),
                                              );
                                            }).toList(),
                                          )
                                        : null,
                                  ),
                                  SizedBox(
                                    height: 5,
                                  ),
                                  Row(
                                    children: <Widget>[
                                      Text(
                                        "导演：",
                                        style: TextStyle(fontSize: 14),
                                      ),
                                      if (detail != null && detail.people != null)
                                        ...detail.people.where((Person person) => person.type == PersonType.DIRECTOR).map((Person person) {
                                          return GestureDetector(
                                            onTap: () {},
                                            child: Text(
                                              person.name,
                                              style: TextStyle(fontSize: 14),
                                            ),
                                          );
                                        }).toList(),
                                    ],
                                  ),
                                ],
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
          if (detail != null)
            SliverList(
              delegate: SliverChildListDelegate(
                <Widget>[
                  Container(
                    child: Padding(
                      padding: EdgeInsets.all(15),
                      child: Text(
                        detail.overview ?? "",
                        style: TextStyle(fontSize: 15),
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(15.0),
                    child: Text(
                      "演职人员",
                      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                    ),
                  ),
                  Container(
                    height: 180,
                    child: ListView(
                      padding: EdgeInsets.only(left: 10),
                      scrollDirection: Axis.horizontal,
                      children: detail.people.map((person) {
                        return Padding(
                          padding: EdgeInsets.all(5),
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              boxShadow: [
                                BoxShadow(color: Colors.grey, blurRadius: 4),
                              ],
                            ),
                            child: CupertinoExtendedImage(
                              "${Util.baseUrl}/Items/${person.id}/Images/Primary",
                              height: 155,
                              width: 120,
                              fit: BoxFit.cover,
                              borderRadius: BorderRadius.circular(10),
                              boxShape: BoxShape.rectangle,
                              placeholder: Container(
                                color: Colors.grey,
                                alignment: Alignment.center,
                                child: Text(
                                  person.name,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        );
                      }).toList(),
                    ),
                  ),
                  SizedBox(
                    height: 300,
                  ),
                ],
              ),
            )
        ],
      ),
    );
  }
}
